<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH. 'controllers/admin/ad_layout.php');

/**
 * @author Nguyen van chi (chinguyen.phpdev@gmail.com)
 * @copyright 2014 - OtcSoft
 */
class Content extends Ad_layout 
{
	public function __construct()
	{
		parent::__construct();
	}
	
	
	/**
	 * Quản trị 
	 */
	public function index()
	{
		/*
		$tabel = "tbl_content";
		$results = $this->utility_model->select($tabel);
		$this->_data['main_container'] = $this->load->view('admin/content/index.phtml', array(
			'results' => $results
		), TRUE);
		$this->load->view('admin/ad_layout_view.phtml', $this->_data);
		
		/*---------------*/
		$config['per_page'] = 15;
		if($this->input->post('search')){
			$search = $this->input->post('search');
			$page = 0;
		}else{
			$search = $this->input->get('search');
		}
		$page = $this->uri->segment(3);
		$result = $this->content_model->getAllSearch($search, $page, $config['per_page']);
		
		
		$this->load->library('pagination');
		
		$config['base_url'] = base_url().'admin/bai-viet';
		$config['total_rows'] = $result['total_record'];
		//echo $result['sql'];
		if($search != ''){
			$this->pagination->suffix = '?search='. $search;
		}
		$config['use_page_numbers'] = false;
		$config['uri_segment'] = 3;
		$config['num_links'] = 1;
		$this->pagination->initialize($config);
		$data["paging"] = $this->pagination->create_links();
		$data['tbl_content'] = $result['tbl_content'];
		$this->_data['main_container'] = $this->load->view('admin/content/index.phtml', array(
				'tbl_content' => $data['tbl_content'],
				//'industry' => $data['industry'],
				'pagination' => $data["paging"],
		), true);
		$this->load->view('admin/ad_layout_view.phtml', $this->_data);
		
		
	} 
	
	
	
	/**
	 * Them moi 
	 */
	public function add()
	{
		
		$this->form_validation->set_rules('content_name', 'Tiêu đề bài viết', 'required');
		$this->form_validation->set_message('required', 'Chưa nhập tên %s.');
		$category = $this->utility_model->getwhere('tbl_menu');
		$dichvu = $this->menu_model->getMenudichvu();
		$menudon = $this->menu_model->getMenuDon();
		$menutintuc = $this->menu_model->getMenutintuc();
		
		
	
		
		if($this->form_validation->run() == FALSE){
				$this->_data['main_container'] = $this->load->view('admin/content/add.phtml', array(
					'menutintuc' => $menutintuc,
					'dichvu' => $dichvu,
					'menudon' => $menudon,
					'category' => $category
				), TRUE);
				$this->load->view('admin/ad_layout_view.phtml', $this->_data);
		}else{
			
			$this->save();
		}
	}
	
	
	/**
	 * Lưu
	 */
	public function save()
	{
		if($this->input->post('menu_type') == 0 && $this->input->post('menu_id') != 999){
			$menu_id =  $this->input->post('menu_id');
		}else if ($this->input->post('menu_type') == 0 && $this->input->post('menu_id') == 999) {
			$menu_id = 0;
		}
		
		if($this->input->post('menu_type') == 1 && $this->input->post('menu_id1') != 999){
			$menu_id =  $this->input->post('menu_id1');
		}else if ($this->input->post('menu_type') == 1 && $this->input->post('menu_id1') == 999) {
			$menu_id = 0;
		}
		
		if($this->input->post('menu_type') == 2  && $this->input->post('menu_id2') != 999){
			$menu_id =  $this->input->post('menu_id2');
		}else if ($this->input->post('menu_type') == 2 && $this->input->post('menu_id2') == 999) {
			$menu_id = 0;
		}
		
		
		$table = 'tbl_content';
		$data = array(
			'content_name' => $this->input->post('content_name'),
			'menu_id' => $menu_id,
			'menu_type' => $this->input->post('menu_type'),
			'img' => $this->input->post('img1'),
			'description' => $this->input->post('description'),
			'metakey' => $this->input->post('metakey'),
			'metades' => $this->input->post('metades'),
			'order' => $this->input->post('order'),
			'status' => $this->input->post('status'),
			'created' => date('Y-m-d H:i:s'),
		);
		$result = $this->utility_model->insert($table , $data);
		if($result){
			redirect('admin/bai-viet');
		}else {
			log_message('error', 'Some variable did not contain a value.');
		}
	}
	
	
	/**
	 * sua
	 */
	public function edit()
	{
		$id=  $this->uri->segment(4);
		$table = 'tbl_content';
		$where = 'content_id';
		$detail = $this->utility_model->getDetail($table, $where, $id);
		$this->form_validation->set_rules('content_name', 'Tiêu đề bài viết', 'required');
		$this->form_validation->set_message('required', 'Chưa nhập tên %s.');
		
		
		$dichvu = $this->menu_model->getMenudichvu();
		$menudon = $this->menu_model->getMenuDon();
		$menutintuc = $this->menu_model->getMenutintuc();
		if($this->form_validation->run() == FALSE){
			$this->_data['main_container'] = $this->load->view('admin/content/edit.phtml', array(
					'detail' => $detail,
					'menutintuc' => $menutintuc,
					'dichvu' => $dichvu,
					'menudon' => $menudon,
			), true);
			$this->load->view('admin/ad_layout_view.phtml', $this->_data);
		}else{
			$this->updated();
		}
	}
	
	
	/**
	 * updated
	 */
	public function updated()
	{
		if($this->input->post('menu_type') == 0 && $this->input->post('menu_id') != 999){
			$menu_id =  $this->input->post('menu_id');
		}else if ($this->input->post('menu_type') == 0 && $this->input->post('menu_id') == 999) {
			$menu_id = 0;
		}
		
		if($this->input->post('menu_type') == 1 && $this->input->post('menu_id1') != 999){
			$menu_id =  $this->input->post('menu_id1');
		}else if ($this->input->post('menu_type') == 1 && $this->input->post('menu_id1') == 999) {
			$menu_id = 0;
		}
		
		if($this->input->post('menu_type') == 2  && $this->input->post('menu_id2') != 999){
			$menu_id =  $this->input->post('menu_id2');
		}else if ($this->input->post('menu_type') == 2 && $this->input->post('menu_id2') == 999) {
			$menu_id = 0;
		}
		
		
		$baseurl = base_url();
		$id = $this->input->post('content_id');
		$where = 'content_id';
		$table = 'tbl_content';
		$data = array(
				'content_name' => $this->input->post('content_name'),
				'menu_id' => $menu_id,
				'menu_type' => $this->input->post('menu_type'),
				'img' => $this->input->post('img1'),
				'description' => $this->input->post('description'),
				'metakey' => $this->input->post('metakey'),
				'metades' => $this->input->post('metades'),
				'order' => $this->input->post('order'),
				'status' => $this->input->post('status'),
				'updated' => date('Y-m-d H:i:s'),
		);
		$result = $this->utility_model->updated($where, $id, $table, $data);
		if($result){
			echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
			<script>
			alert('Bạn đã cập nhật thành công');
			window.open('{$baseurl}admin/bai-viet.html','_self');
			</script>";
		}else {
		log_message('error', 'Some variable did not contain a value.');
		}
	}
	
	
	/**
	* Delete
	*/
	public function delete()
	{
		$where = 'content_id';
		$table = 'tbl_content';
		$baseurl = base_url();
		$id_list_str = $this->input->post('id_list');
		$id_dels = explode(",",$id_list_str);
		foreach($id_dels as $id_del){
			$result = $this->utility_model->delete($table, $where , $id_del);
		}
		if($result){
		echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
			<script>
			alert('Bạn đã xóa thành công');
			window.open('{$baseurl}admin/bai-viet.html','_self');
			</script>";
		}else {
		log_message('error', 'Some variable did not contain a value.');
	}
	}
	
	
}